'use strict';
app.controller('tagCtrl',['$scope','$state','tagService','utilService','toaster','$modal',function($scope,$state,tagService,utilService,toaster,$modal){
    $scope.findAll=function(){
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'tag'
        }).then(function (result) {
        });
    }

    $scope.findAll();

    $scope.updateToDelete=function(item){
        //tagService.updateToDelete($scope,id).then(function(result){
        //    switch (result.status){
        //        case 200:
        //            toaster.pop('success', "提示", "操作成功");
        //            break;
        //        default :
        //            toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
        //            break;
        //    }
        //});
        openModal(item);
    }

    function openModal(msg){
        utilService.modal($modal, $scope, {
            title:"删除标签",
            controller:"deleteTagCtrl",
            message:msg.name,
            data:msg
        });
    }

}]);

app.controller('tagAddCtrl',['$scope','$state','tagService',function($scope,$state,tagService){
    $scope.tagService=tagService;

    $scope.addState=false;

    $scope.init=function(){
        tagService.tag={target:0, type:0};
    }

    $scope.init();

    $scope.add=function(){
        if($scope.addState){
            return;
        }

        $scope.addState=true;

        tagService.add($scope).then(function(result){
            switch (result.status){
                case 200:
                    $state.go("app.tag");
                    $scope.addState=false;
                    toaster.pop('success', "提示", "操作成功");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

}]);

app.controller('tagEditCtrl',['$scope','$state','$stateParams','tagService',function($scope,$state,$stateParams,tagService){
    $scope.tagId=$stateParams.id;

    $scope.tagService=tagService;
    $scope.editState=false;

    $scope.findById=function(){
        tagService.findById($scope).then(function(result){
            switch (result.status){
                case 200:
                    console.log(result.data);
                    tagService.tag=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.findById();

    $scope.edit=function(){
        if($scope.editState){
            return;
        }

        $scope.editState=true;

        tagService.edit($scope).then(function(result){
            switch (result.status){
                case 200:
                    $state.go("app.tag");
                    toaster.pop('success', "提示", "操作成功");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

}]);


app.controller('deleteTagCtrl', ['$scope', '$modalInstance','$state','tagService','toaster','modalData',
    function($scope, $modalInstance,$state,tagService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.isDelModal=true;

        //确认删除
        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;

            tagService.updateToDelete($scope,modalData.data.id).then(function(result){
                switch (result.status){
                    case 200:
                        $scope.isDelModal=true;
                        $scope.findAll();
                        $modalInstance.dismiss('success');
                        break;
                    case 615:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.isDelModal=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);
